package com.hlc.helloWord.common.exception;

import com.hlc.helloWord.common.result.ResultEnum;
import com.hlc.helloWord.common.result.ResultInfo;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by helinchong on 2017/12/28.
 */
@ControllerAdvice
public class ExceptionHandlers {

    private Logger logger = Logger.getLogger(ExceptionHandlers.class);

    @ExceptionHandler(value = MyException.class)
    @ResponseBody
    public ResultInfo handler(MyException e){
        if(e instanceof MyException){
            MyException myException = (MyException)e;
            e.printStackTrace();
            return new ResultInfo().setCode(myException.getCode()).setMsg(myException.getMessage());
        }else{
            logger.error(e.getMessage());
            e.printStackTrace();
            return new ResultInfo().setCode(ResultEnum.UNKNOWN_EXCETION.getCode()).setMsg(ResultEnum.UNKNOWN_EXCETION.getValue());
        }
    }
}
